﻿Public Class Monedas

    Private oMaestra As New CMaestras
    Private idMoneda As Integer

    Private Sub Monedas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CargarGrilla()
        Me.CargarPaises()
    End Sub

    Private Sub CargarGrilla()
        Try
            Dim ds As New DataSet
            ds = Me.oMaestra.GetTabla(CMaestras.Tabla.Moneda)
            'Llena grilla de las monedas
            If (ds.Tables(0).Rows.Count > 0) Then
                Me.grdMoneda.DataSource = Nothing
                Me.grdMoneda.Columns.Clear()
                Me.grdMoneda.DataSource = ds.Tables(0)

                Dim iconoEditar As New DataGridViewImageColumn()
                iconoEditar.ValuesAreIcons = False
                iconoEditar.Image = My.Resources.editar
                iconoEditar.HeaderText = "EDITAR"
                iconoEditar.Name = "EDITAR"
                Me.grdMoneda.Columns.Add(iconoEditar)

                Dim iconoEliminar As New DataGridViewImageColumn()
                iconoEliminar.ValuesAreIcons = False
                iconoEliminar.Image = My.Resources.eliminar
                iconoEliminar.HeaderText = "ELIMINAR"
                iconoEliminar.Name = "ELIMINAR"
                Me.grdMoneda.Columns.Add(iconoEliminar)

                Me.grdMoneda.Columns("ID").Visible = False
                Me.grdMoneda.Columns("IDPAIS").Visible = False
                Me.grdMoneda.Columns("EDITAR").Width = 60
                Me.grdMoneda.Columns("ELIMINAR").Width = 70
            Else
                Me.grdMoneda.DataSource = Nothing
                Me.grdMoneda.Columns.Clear()
                MessageBox.Show("No existen monedas", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

        Catch ex As Exception
            MessageBox.Show("Ocurrió un error al cargar las monedas: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub CargarPaises()
        Dim ds As New DataSet
        ds = Me.oMaestra.GetCiudades()

        'Llena grilla de los paises
        If (ds.Tables(1).Rows.Count > 0) Then
            Me.drpPais.DataSource = Nothing
            Me.drpPais.Items.Clear()

            Me.drpPais.DataSource = CUtil.AgregarRegistro(ds.Tables(1), "Seleccione...")
            Me.drpPais.ValueMember = ds.Tables(1).Columns(0).ColumnName
            Me.drpPais.DisplayMember = ds.Tables(1).Columns(1).ColumnName
        Else
            Me.drpPais.DataSource = Nothing
            Me.drpPais.Items.Clear()
            MessageBox.Show("No existen países", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

    End Sub

    Private Sub EliminarMoneda(ByVal id As Integer)
        Try
            If (MessageBox.Show("¿Realmente desea eliminar esta moneda?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
                Cursor = Cursors.WaitCursor
                Me.oMaestra.Eliminar(id, CMaestras.Tabla.Moneda)
                MessageBox.Show("La moneda ha sido eliminada", "Moneda eliminada", MessageBoxButtons.OK, MessageBoxIcon.Information)

                Cursor = Cursors.Default
            End If
        Catch ex As Exception
            Cursor = Cursors.Default
            MessageBox.Show("NO se eliminó la moneda: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
        Me.CargarGrilla()
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.idMoneda = 0
        Me.txtMoneda.Text = ""
        If (Me.drpPais.Items.Count = 0) Then
            Me.drpPais.Items.Add("No hay registros")
        End If
        Me.drpPais.SelectedIndex = 0
    End Sub

    Private Sub btnListo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListo.Click
        If (Me.ValidarMoneda()) Then
            Try
                Cursor = Cursors.WaitCursor

                Dim result As String = Me.oMaestra.ActualizarMoneda(Me.idMoneda, Me.txtMoneda.Text, CInt(Me.drpPais.SelectedValue))
                Cursor = Cursors.Default

                MessageBox.Show(result, "Moneda", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex As Exception
                Cursor = Cursors.Default
                MessageBox.Show("Ocurrió un error al guardar la moneda: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End Try

            Me.idMoneda = 0
            Me.txtMoneda.Text = ""
            Me.CargarGrilla()
        End If
    End Sub

    Private Function ValidarMoneda() As Boolean
        Dim mensaje As String = ""

        If (Trim(Me.txtMoneda.Text).Length = 0) Then
            mensaje += "- Debe escribir la moneda" & Chr(13)
        End If
        If (CInt(Me.drpPais.SelectedValue) = 0) Then
            mensaje += "- Debe seleccionar el país" & Chr(13)
        End If
        If (mensaje.Length > 0) Then
            MessageBox.Show(mensaje, "Datos incompletos", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Return False
        Else
            Return True
        End If
    End Function

    Private Sub grdMoneda_CellMouseEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdMoneda.CellMouseEnter
        '*** Coloca la manito cuando el mouse entra en las celdas de EDITAR y ELIMINAR
        If (Me.grdMoneda.Columns(e.ColumnIndex).Name = "ELIMINAR" Or Me.grdMoneda.Columns(e.ColumnIndex).Name = "EDITAR") Then
            Cursor = Cursors.Hand
        Else
            Cursor = Cursors.Default
        End If
    End Sub

    Private Sub grdMoneda_CellMouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles grdMoneda.CellMouseMove
        If (Me.grdMoneda.Columns(e.ColumnIndex).Name = "ELIMINAR" Or Me.grdMoneda.Columns(e.ColumnIndex).Name = "EDITAR") Then
            Cursor = Cursors.Hand
        Else
            Cursor = Cursors.Default
        End If
    End Sub

    Private Sub grdMoneda_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdMoneda.CellDoubleClick
        '*** Permite EDITAR o ELIMINAR
        'Se obtiene la info del registro
        Dim id As Integer = Me.grdMoneda.Rows(e.RowIndex).Cells("ID").Value
        Dim moneda As String = Me.grdMoneda.Rows(e.RowIndex).Cells("MONEDA").Value
        Dim pais As Integer = Me.grdMoneda.Rows(e.RowIndex).Cells("IDPAIS").Value

        Select Case Me.grdMoneda.Columns(e.ColumnIndex).Name
            Case "EDITAR"
                Me.idMoneda = id
                Me.txtMoneda.Text = moneda
                Me.drpPais.SelectedValue = pais
            Case "ELIMINAR"
                Me.EliminarMoneda(id)
        End Select
    End Sub
End Class